package libjdc.dc.client;
import hubsniffer.*;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/*
 * Created on 8/Mai/2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author Daniel Botelho
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class UserList extends TreeSet<User>{
	
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	
	public UserList(Comparator<User> c) {
		super(c);
		// TODO Auto-generated constructor stub
	}
        
        public UserList(){
            super();
        }
        
        public boolean add(User u){
            User t = this.getUser(u.getUserInfo().getNick());
            
            if(t != null)
                t.setUserInfo(u.getUserInfo());
            else
                return super.add(u);
            return true;
        }

	
	public User getUser(String nick){
		Iterator it = this.iterator();
		while(it.hasNext()){
			User u = ((User)it.next());
			if(u.getUserInfo().getNick().equals(nick))
				return u;
		}
		return null;
	}
	
	public boolean remove(String nick){
		Iterator it = this.iterator();
		while(it.hasNext()){
			User u = ((User)it.next());
			if(u.getUserInfo().getNick().equals(nick)){
				return super.remove(u);
			}
		}
		return false;
	}
	/**
	 * @return Returns the userCount.
	 */
	public long getUserCount() {
		return this.size();
	}
	/**
	 * @return Returns the userShare.
	 */
	public long getUserShare() {
		long userShare=0;
		Iterator<User> it = iterator();
		while(it.hasNext())
			userShare+=it.next().getUserInfo().getSharedBytes();
		return userShare;
	}
}
